package com.syni.mdd.sjb.marketing.basemarket.service.commom;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.utils.common.service.UtilsCommonService;
import com.syni.mdd.sjb.marketing.basemarket.entity.bo.dist.FakeDataBo;
import com.syni.mdd.sjb.marketing.basemarket.entity.bo.luckDraw.LuckDrawFakeDataBo;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

@Service
@Data
@Log4j2
public class CommonService {
    @Value("${cps.api.fake.data.status:0}")
    private boolean apiFakeDataStatus;

    @Value("${cps.api.fake.dist.fake.data}")
    private String distFakeData;

    @Value("${cps.api.fake.luckdraw.fake.data}")
    private String luckdrawFakeData;

    public static FakeDataBo FAKE_DATA;

    public static LuckDrawFakeDataBo LUCK_DRAW_DATA;

    @Autowired
    UtilsCommonService utilsCommonService;

    @PostConstruct
    public void init(){
        initFakeData();
    }

    public void initFakeData(){
        String jsonCode = null;
        try {
            jsonCode = IOUtils.toString(new InputStreamReader(new FileInputStream(new File(distFakeData)),"UTF-8"));
            FAKE_DATA = JSON.parseObject(jsonCode, FakeDataBo.class);
            jsonCode = IOUtils.toString(new InputStreamReader(new FileInputStream(new File(luckdrawFakeData)),"UTF-8"));
            LUCK_DRAW_DATA = JSON.parseObject(jsonCode, LuckDrawFakeDataBo.class);
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        }
    }
}
